from django.contrib import admin
from django.contrib.auth import admin as auth_admin
from django.contrib.auth import get_user_model
from django.utils.translation import gettext_lazy as _
from backend.users.models import GroupSetting
from backend.gnsuser.models import GnsUser

from backend.users.forms import UserAdminChangeForm, UserAdminCreationForm

User = get_user_model()

class GnsUserInline(admin.StackedInline):
    model = GnsUser
    can_delete = False
    verbose_name_plural = 'GnsUser'



@admin.register(User)
class UserAdmin(auth_admin.UserAdmin):
    form = UserAdminChangeForm
    add_form = UserAdminCreationForm
    fieldsets = (
        (None, {"fields": ("username", "password")}),
        (_("Personal info"), {"fields": ("name", "email", "enable_email")}),
        (
            _("Permissions"),
            {
                "fields": (
                    "is_active",
                    "is_staff",
                    "is_superuser",
                    "groups",
                    "user_permissions",
                ),
            },
        ),
        (_("Important dates"), {"fields": ("last_login", "date_joined")}),
    )
    list_display = ["username", "name" , "email", "is_superuser", "enable_email"]
    search_fields = ["username"]
    inlines = [GnsUserInline]


# class GroupSettingInline(admin.StackedInline):
#     model = GroupSetting


# class GroupSettingAdmin(admin.ModelAdmin):
#     model = GroupSetting
#     # inlines = [
#     #     GroupSettingInline,
#     # ]
#     filter_horizontal = ("users",)
#     def formfield_for_manytomany(self, db_field, request=None, **kwargs):
#         if db_field.name == "users":
#             qs = kwargs.get("queryset", db_field.remote_field.model.objects)
#             # Avoid a major performance hit resolving permission names which
#             # triggers a content_type load:
#             # kwargs["queryset"] = qs.select_related("content_type")
#             kwargs["queryset"] = qs
#         return super().formfield_for_manytomany(db_field, request=request, **kwargs)

# admin.site.register(GroupSetting,GroupSettingAdmin)
